/*
 * > /*
>  * INSANE - Interactive Structural Analysis Environment
>  *
>  * Copyright (C) 2003-2004
>  * Universidade Federal de Minas Gerais
>  * Escola de Engenharia
>  * Departamento de Engenharia de Estruturas
>  *
>  * Author's email :     insane@dees.ufmg.br
>  * Author's Website :   http://www.dees.ufmg.br/insane
>  *
>  * This program is free software; you can redistribute it and/or
>  * modify it under the terms of the GNU General Public License
>  * as published by the Free Software Foundation; either version 2
>  * of the License, or any later version.
>  *
>  * This program is distributed in the hope that it will be useful,
>  * but WITHOUT ANY WARRANTY; without even the implied warranty of
>  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>  * GNU General Public License for more details.
>  *
>  * You should have received a copy of the GNU General Public License
>  * along with this program; if not, write to the Free Software
>  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
> 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view.dialog;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;

import br.ufmg.dees.insane.commons.view.ModelList;
import br.ufmg.dees.insane.commons.view.ViewState;
import br.ufmg.dees.insane.model.postp.PostpModel;
import br.ufmg.dees.insane.model.postp.StepPostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.DesktopPostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;

/**
 * A class representing creation dialog on screen.<br>
 * @author Penna, Samuel & Pitangueira, Roque
 * @since september 2004
 */
public final class ModelSelectionDialog extends SimpleDialog {

    /**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private DesktopPostp desktop;
    
	private JPanel modelPane = new JPanel();
	private JPanel buttonPane = new JPanel();
	private JList panelList;
	
	private JButton viewButton;
	private JButton closeButton;
	private JButton delButton;
	private JScrollPane scroll;
	
	private String[] keysList;
	
	private PostpModel postpModel;
    private NumberFormat numberFormat;
    //private ViewState state;
	
//*****************************************************************************
	
    /**
     * Constructor method, calling super with the especific title.
     *
     * @param parent The parent of the class.
     * @see gui.Interface
     */
    public ModelSelectionDialog(DesktopPostp parent, ViewState s) {
        this.desktop = parent;
        //state = s;
        keysList = new String[desktop.getModelList().getModelList().size()]; 
        for(int i=0;i<keysList.length;i++){
        	keysList[i] = (String)desktop.getModelList().getKeys().get(i);
        }
        setUpFormat();
		
        // Sets up the grid panel
        JPanel mainPane = new JPanel();
        mainPane.setLayout(new BoxLayout(mainPane, BoxLayout.Y_AXIS));
        mainPane.add(modelPane);
        modelPane.setLayout(new GridLayout(1,2,2,2));
        modelPane.setPreferredSize(new Dimension(300,300));
        modelPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder("Selction List"),
        BorderFactory.createEmptyBorder(2,2,2,2)));
        
        panelList = new JList(keysList);
		panelList.setSize(200,300);
		panelList.setBackground(Color.BLACK);
		panelList.setForeground(Color.GREEN);
		panelList.setVisibleRowCount(5);
		panelList.setFixedCellHeight(20);
		panelList.setFixedCellWidth(300);
		panelList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		panelList.setAutoscrolls(true);

		scroll = new JScrollPane(panelList);
        
		modelPane.add(scroll);
        
		mainPane.add(buttonPane);
		buttonPane.setLayout(new GridLayout(1,3,1,1));
		buttonPane.setPreferredSize(new Dimension(10,50));
		buttonPane.setBorder(BorderFactory.createCompoundBorder(
	    BorderFactory.createTitledBorder(""),
	    BorderFactory.createEmptyBorder(10,10,5,5)));
		viewButton = new JButton("View");
		closeButton = new JButton("Close");
		delButton = new JButton("Delete");
		buttonPane.add(viewButton);
		buttonPane.add(closeButton);
		buttonPane.add(delButton);
        setPane(mainPane);
        //setPane(buttonPane);
		
        viewButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						
						if(panelList.getSelectedValues().length!=0){
							postpModel = (PostpModel)((ModelList)desktop.getModelList()).getModel((String)panelList.getSelectedValues()[0]);
							InterfacePostp intFrame = new InterfacePostp();
					 	    intFrame.validate();
					 	    intFrame.setPostpModel(postpModel);
					 	    intFrame.setModel((StepPostp)postpModel.getStepList().get(0));
					 	    intFrame.setTitle(postpModel.getLabel());
					 	    ((DesktopPostp)desktop).addInternalFrame(intFrame);
					 	    intFrame.toFront();
						}else{
							String message = "You must select a model!";
							JOptionPane.showMessageDialog(desktop, message, "Error Message", JOptionPane.ERROR_MESSAGE);
						}
					}
				});
        
        closeButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						if(panelList.getSelectedValues().length!=0){
							ArrayList<JInternalFrame> intFramesTemp = new ArrayList<JInternalFrame>();						
							for(int i=0;i<desktop.getDesktop().getAllFrames().length;i++){
								if(!((InterfacePostp)desktop.getDesktop().getAllFrames()[i]).getPostpModel().getLabel().equals((String)panelList.getSelectedValues()[0])){
									intFramesTemp.add(desktop.getDesktop().getAllFrames()[i]);
								}
							}
							desktop.getDesktop().removeAll();
							for(int i=0;i<intFramesTemp.size();i++){
								desktop.getDesktop().add((InterfacePostp)intFramesTemp.get(i));
							}
							desktop.getDesktop().repaint();
						}else{
							String message = "You must select a model!";
							JOptionPane.showMessageDialog(desktop, message, "Error Message", JOptionPane.ERROR_MESSAGE);
						}
					}
				});
        
        delButton.addActionListener(
				new ActionListener(){
					public void actionPerformed(ActionEvent event){
						if(panelList.getSelectedValues().length!=0){
							ArrayList<JInternalFrame> intFramesTemp = new ArrayList<JInternalFrame>();						
							for(int i=0;i<desktop.getDesktop().getAllFrames().length;i++){
								System.out.println(((InterfacePostp)desktop.getDesktop().getAllFrames()[0]).getPostpModel().getLabel());
								System.out.println(((String)panelList.getSelectedValues()[0]));
								if(!((InterfacePostp)desktop.getDesktop().getAllFrames()[i]).getPostpModel().getLabel().equals((String)panelList.getSelectedValues()[0])){
									intFramesTemp.add(desktop.getDesktop().getAllFrames()[i]);
								}
							}
							desktop.getDesktop().removeAll();
							for(int i=0;i<intFramesTemp.size();i++){
								desktop.getDesktop().add((InterfacePostp)intFramesTemp.get(i));
							}
							desktop.getDesktop().repaint();
							
							((ModelList)desktop.getModelList()).removeModel((String)panelList.getSelectedValues()[0]);
							keysList = new String[desktop.getModelList().getModelList().size()]; 
							for(int i=0;i<keysList.length;i++){
								keysList[i] = (String)desktop.getModelList().getKeys().get(i);
							}
							panelList.setListData(keysList);
							panelList.repaint();
						}else{
							String message = "You must select a model!";
							JOptionPane.showMessageDialog(desktop, message, "Error Message", JOptionPane.ERROR_MESSAGE);
						}
					
					
					
					}		
				});
    }
	
//*****************************************************************************
    
    public int showDialog() {
        return super.showDialog(desktop , "Model Selection");
    }
	
//*****************************************************************************
	
    /**
     * This method is executed on OK button click.
     *
     */
    public void approvedPerform() {
        try {
			
        } catch (Exception err) {
            Toolkit.getDefaultToolkit().beep();
            this.setVisible(false);
        }
    }
    
//*****************************************************************************
    /**
     * Sets the number formater
     */
    private void setUpFormat() {
        numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
        numberFormat.setMaximumFractionDigits(3);
    }
	
//*****************************************************************************
}
